﻿using MasterMasterMode.Common.Systems;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Localization;

namespace MasterMasterMode.Content.Invasions
{
    public class ArmyofwarriorgoldfishesInvasion
    {
        public static int[] invaders = [];
        public static void StartCustomInvasion()
        {
            if (Main.invasionType != 0 && Main.invasionSize == 0)
            {
                Main.invasionType = 0;
            }
            if (Main.invasionType == 0)
            {
                int numPlayers = 0;
                for (int i = 0; i < 255; i++)
                {
                    if (Main.player[i].active && Main.player[i].statLifeMax >= 200)
                    {
                        numPlayers++;
                    }
                }
                if (numPlayers > 0)
                {
                    Main.invasionType = -1;
                    InvasionSystem.ArmyofwarriorgoldfishesUp = true;
                    Main.invasionSize = 100 * numPlayers;
                    Main.invasionSizeStart = Main.invasionSize;
                    Main.invasionProgress = 0;
                    Main.invasionProgressIcon = -1;
                    Main.invasionProgressWave = 0;
                    Main.invasionProgressMax = Main.invasionSizeStart;
                    Main.invasionWarn = 3600;
                    if (Main.rand.Next(2) == 0)
                    {
                        Main.invasionX = 0.0;
                        return;
                    }
                    Main.invasionX = Main.maxTilesX;
                }
            }
        }
        public static void CustomInvasionWarning()
        {
            string text = "";
            if (Main.invasionX == Main.spawnTileX)
            {
                text = Language.GetTextValue("Mods.MasterMasterMode.Invasion.1");
            }
            if (Main.invasionSize == 0)
            {
                text = Language.GetTextValue("Mods.MasterMasterMode.Invasion.2");
            }
            if (Main.netMode == 0)
            {
                Main.NewText(text, 175, 75, byte.MaxValue);
                return;
            }
            if (Main.netMode == 2)
            {
                NetMessage.SendData(25, -1, -1, NetworkText.FromLiteral(text), 255, 175f, 75f, 255f, 0, 0, 0);
            }
        }
        public static void UpdateCustomInvasion()
        {
            if (InvasionSystem.ArmyofwarriorgoldfishesUp)
            {
                if (Main.invasionSize <= 0)
                {
                    InvasionSystem.ArmyofwarriorgoldfishesUp = false;
                    InvasionSystem.DownedArmyofwarriorgoldfishes = true;
                    CustomInvasionWarning();
                    Main.invasionType = 0;
                    Main.invasionDelay = 0;
                }
                if (Main.invasionX == Main.spawnTileX)
                {
                    return;
                }
                float moveRate = (float)Main.dayRate;
                if (Main.invasionX > Main.spawnTileX)
                {
                    Main.invasionX -= moveRate;
                    if (Main.invasionX <= Main.spawnTileX)
                    {
                        Main.invasionX = Main.spawnTileX;
                        CustomInvasionWarning();
                        return;
                    }
                    Main.invasionWarn--;
                    return;
                }
                else if (Main.invasionX < Main.spawnTileX)
                {
                    Main.invasionX += moveRate;
                    if (Main.invasionX >= Main.spawnTileX)
                    {
                        Main.invasionX = Main.spawnTileX;
                        CustomInvasionWarning();
                        return;
                    }
                    Main.invasionWarn--;
                }
            }
        }
        public static void CheckCustomInvasionProgress()
        {
            if (Main.invasionProgressMode != 2)
            {
                Main.invasionProgressNearInvasion = false;
                return;
            }
            bool flag = false;
            Rectangle rectangle;
            rectangle = new((int)Main.screenPosition.X, (int)Main.screenPosition.Y, Main.screenWidth, Main.screenHeight);
            int icon = 0;
            for (int i = 0; i < 200; i++)
            {
                if (Main.npc[i].active)
                {
                    icon = 0;
                    int type = Main.npc[i].type;
                    for (int j = 0; j < invaders.Length; j++)
                    {
                        if (type == invaders[j])
                        {
                            Rectangle value;
                            value = new((int)(Main.npc[i].position.X + Main.npc[i].width / 2) - 5000, (int)(Main.npc[i].position.Y + (float)(Main.npc[i].height / 2)) - 5000, 10000, 10000);
                            if (rectangle.Intersects(value))
                            {
                                flag = true;
                                break;
                            }
                        }
                    }
                }
            }
            Main.invasionProgressNearInvasion = flag;
            int progressMax3 = 1;
            if (InvasionSystem.ArmyofwarriorgoldfishesUp)
            {
                progressMax3 = Main.invasionSizeStart;
            }
            if (InvasionSystem.ArmyofwarriorgoldfishesUp && Main.invasionX == Main.spawnTileX)
            {
                Main.ReportInvasionProgress(Main.invasionSizeStart - Main.invasionSize, progressMax3, icon, 0);
            }
            foreach (Player p in Main.player)
            {
                NetMessage.SendData(78, p.whoAmI, -1, null, Main.invasionSizeStart - Main.invasionSize, Main.invasionSizeStart, Main.invasionType + 3, 0f, 0, 0, 0);
            }
        }
    }
}
